﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.CompilerServices;

namespace SocketClient
{
    public class Conexao
    {
        private StreamReader reader, readerRetorno;
        private StreamWriter writer;
        private TcpClient socket, socketRetorno;

        public String NomeJogador
        {
            get;
            private set;
        }

        public Conexao(String nomeJogador)
        {
            socket = new TcpClient();
            socket.Connect("10.0.0.11", 6654);
            socketRetorno = new TcpClient();
            socketRetorno.Connect("10.0.0.11", 6654);
            NetworkStream networkStream = socket.GetStream();
            reader = new StreamReader(networkStream, Encoding.ASCII);
            writer = new StreamWriter(networkStream, Encoding.ASCII);
            writer.AutoFlush = true;
            networkStream = socketRetorno.GetStream();
            readerRetorno = new StreamReader(networkStream, Encoding.ASCII);
            this.NomeJogador = nomeJogador;
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        public void Send(String msg)
        {
            writer.WriteLine(msg);
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        public String Receive()
        {
            return reader.ReadLine();
        }

        public String ReceiveRetorno()
        {
            return readerRetorno.ReadLine();
        }

        public void close()
        {
            readerRetorno.Close();
            reader.Close();
            writer.Close();
            socket.Close();
        }
    }
}
